package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-21
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    /**
     * 保存作者
     * @param apAuthor
     * @return
     */
    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor apAuthor) {
        //关键数据校验 如果为空，返回无效
        if (apAuthor==null ||apAuthor.getUserId()==null ||apAuthor.getWmUserId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否有记录 判断user_id对应的记录
        //根据user_id来查询
        LambdaQueryWrapper<ApAuthor> query = new LambdaQueryWrapper<>();
        query.eq(ApAuthor::getUserId,apAuthor.getUserId());
        ApAuthor one = this.getOne(query);
        //如果存在作者，返回
        if (one != null){
            return ResponseResult.okResult(one);
        }
        //不存在则新增，返回新增作者的信息
        apAuthor.setCreatedTime(new Date());//设置创建时间为当前时间
        this.save(apAuthor);
        return  ResponseResult.okResult(apAuthor);
    }
}
